fix: remove TxHash caching to prevent stale hash after mutation#107
Merged
fix: remove TxHash caching to prevent stale hash after mutation#107
Conversation
The cachedHash field on MsgTx caused two issues: - reflect.DeepEqual comparisons failed when one MsgTx had been hashed and the other had not - In-place transaction sorting (e.g. BIP 69) returned stale cached hashes since the cache was not invalidated on slice reordering Profiling showed caching provided no measurable benefit since each transaction hash is typically computed only once. The streaming hash optimization (writing directly to sha256.New()) is preserved.
5475e8d to
c34b9fe
Compare
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Summary
cachedHashfield fromMsgTxthat was introduced in v1.2.0sha256.New()instead of intermediate buffer)Problem
The
cachedHashfield caused two issues:Stale hash after in-place mutation —
txsort.InPlaceSort(BIP 69) reorders inputs/outputs on an existingMsgTx, but the cached hash was not invalidated sincecachedHashis unexported. SubsequentTxHash()calls returned the pre-sort hash.reflect.DeepEqualfailures — Any code comparingMsgTxstructs viareflect.DeepEqualwould fail if one had been hashed (populating the cache) and the other had not.Why removal is safe
Profiling on a 3.64 GB testnet block (28,672 transactions) confirmed caching provided zero measurable benefit — each transaction hash is typically computed only once during block processing. Results with and without caching are identical:
The real optimization was the streaming hash (eliminating the intermediate
bytes.Buffer), which is preserved.Test plan
TestHandleBlockDirect_TestnetLargeBlockpasses with no regression